/**
 * ImportController.java
 * Version 1.0.0
 * Created on 2018年11月7日
 *
 */
package com.yincheng.oes.business.base.importexport;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * @author yangsy
 * @version 1.0.0
 * @since
 * 创建时间：2018年11月7日
 * <p>
 * 功能描述：
 * <p>
 * 邮箱地址：446948434@qq.com
 */
@Controller
@RequestMapping("/import")
public class ImportController {

    @Autowired
    private UploadQuestionImpl uploadService;

    @RequestMapping(value = "/uploadQuestions", method = RequestMethod.POST)
    @ResponseBody
    public Response uploadQuestions(HttpServletRequest request, HttpServletResponse response) {
        MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
        Response result = new Response();
        for (Map.Entry<String, MultipartFile> entry : mRequest.getFileMap().entrySet()) {
            MultipartFile file = entry.getValue();

            try {
                result = uploadService.batchQuestionUpload(file.getInputStream(), file.getOriginalFilename());
            } catch (DuplicateKeyException e) {
                result.setCode(Response.SERVER_ERROR);
                result.setMessage(e.getCause().getMessage());
                result.setErrorMessage(e.getMessage());
            } catch (Exception e) {
                result.setCode(Response.SERVER_ERROR);
                String message = e.getCause() == null ? e.getMessage() : e.getCause().getMessage();
                result.setMessage(message);
                result.setErrorMessage(e.getMessage());
            }
            continue;
        }
        return result;
    }
}
